﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Script.Serialization;

namespace BlackBird.Resources
{
	public class ClientResources
	{
		private static UrlHelper helper;

		public static string GetClientResources(RequestContext context)
		{
			helper = new UrlHelper(context);

			var resources = new
			{
                jsonactions = LoadActions2(new[]
				{
				    new Tuple<string,ActionResult,string>("get_all_products",MVC.BiJiaDan.GetAllProducts(),"get"),
                    new Tuple<string,ActionResult,string>("get_all_suppliers",MVC.BiJiaDan.GetAllSuppliers(),"get"),
                    new Tuple<string,ActionResult,string>("get_product_unit",MVC.BiJiaDan.GetProductUnit(),"get"),
                    new Tuple<string,ActionResult,string>("get_supplier",MVC.BiJiaDan.GetSupplier(),"get"),
					new Tuple<string,ActionResult,string>("delete_project_plan_item",MVC.Project.DeleteProjectPlan(),"post"),
					new Tuple<string,ActionResult,string>("submit_project_plan",MVC.Project.SubmitProjectPlan(),"post")
				    //new Tuple<string,ActionResult,string>("translation_statuses",MVC.Assets.TranslationStatuses(),"get"),
				    //new Tuple<string,ActionResult,string>("save_termsofuse",MVC.TermsOfUse.SaveInfo(),"post"),
				    //new Tuple<string,ActionResult,string>("get_inprocessids",MVC.Assets.InProcessAssetIds(),"get")
				}),
				partialviewactions = LoadActions2(new[]
				{
				    new Tuple<string,ActionResult,string>("new_bijiadan",MVC.BiJiaDan.NewBiJiaDan(),"get"),
					new Tuple<string,ActionResult,string>("new_product_view",MVC.Common.NewProduct(),"get"),
					new Tuple<string,ActionResult,string>("new_supplier_view",MVC.Common.NewSupplier(),"get"),
					new Tuple<string,ActionResult,string>("new_project_view",MVC.Project.NewProject(),"get"),
					new Tuple<string,ActionResult,string>("new_income_view",MVC.IncomeExpend.AddIncomeAndExpend(),"get"),
					new Tuple<string,ActionResult,string>("add_project_plan_view",MVC.Project.AddPlanPartialView(),"get"),
					new Tuple<string,ActionResult,string>("upload_file_view",MVC.FileUpload.UploadFilePartialView(),"get"),
					new Tuple<string,ActionResult,string>("shenhe_comment_view",MVC.Project.ShenHePartialView(),"get"),
					new Tuple<string,ActionResult,string>("get_attached_files",MVC.BiJiaDan.GetBiJiaDanAttachedFile(),"get"),
					new Tuple<string,ActionResult,string>("get_changepassword_view",MVC.Account.ChangePassword(),"get")
				    //new Tuple<string,ActionResult,string>("assets",MVC.Assets.Assets(),"get")  
				}),
				emptyactions = LoadActions2(new[] 
				{
				    new Tuple<string,ActionResult,string>("file_upload",MVC.FileUpload.UploadFiles(),"post"),
				    new Tuple<string,ActionResult,string>("project_plan",MVC.Project.PlanProject(),"get"),
					new Tuple<string,ActionResult,string>("sample_contracts",MVC.Contract.ExampleContracts(),"get"),
					new Tuple<string,ActionResult,string>("project_contracts",MVC.Contract.ProjectContracts(),"get"),
                    new Tuple<string,ActionResult,string>("create_fukuandan",MVC.FuKuanDan.Create(), "get")
				}),
				constants = GetJavascriptConstants()
			};

			return (new JavaScriptSerializer()).Serialize(resources);
		}


		private static Dictionary<string, string> GetJavascriptConstants()
		{
			var result = new Dictionary<string, string>();

			var resourceMembers = typeof(BBJavascriptStrings).GetMembers();
			foreach (var prop in resourceMembers)
			{
				if (prop.GetType().GetProperty("PropertyType") != null)
				{
					var type = prop.GetType().GetProperty("PropertyType").GetValue(prop, null);
					var typeName = type.GetType().GetProperty("Name").GetValue(type, null);
					if (typeName.Equals("String"))
					{
						result.Add(prop.Name, (string)typeof(BBJavascriptStrings).GetProperty(prop.Name).GetValue(null, null));
					}
				}
			}

			return result;
		}

		private static Dictionary<string, dynamic> LoadActions2(Tuple<string, ActionResult, string>[] actions)
		{
			var res = new Dictionary<string, dynamic>();
			foreach (var action in actions)
			{
				res.Add(action.Item1, new
				{
					url = helper.Action(action.Item2.GetRouteValueDictionary()["Action"] as string, action.Item2.GetRouteValueDictionary()["Controller"] as string),
					httpverb = action.Item3
				});
			}
			return res;
		}
	}
}